package com.whut.whw.personalinfo;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.whut.whw.R;
import com.whut.whw.personalinfo.mythings.MyThingsActivity;
import com.whut.whw.util.GlideCircularLoadUtils;

import java.util.List;

/**
 * Created by 曾旺红 on 2017/06/09.
 */

public class PersonalInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Object> list;

    public static final int TYPE_HEADER = 0;
    public static final int TYPE_CONTENT = 1;
    public static final int TYPE_SPLIT = 2;
    public static Context mcontext;

    //点击
    private OnItemClickListener mOnItemClickListener;
    //长按
    private OnItemLongClickListener mOnItemLongClickListener;

    //点击
    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    //长按
    public void setmOnItemLongClickListener(OnItemLongClickListener mOnItemLongClickListener) {
        this.mOnItemLongClickListener = mOnItemLongClickListener;
    }

    //点击监听接口
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    //长按监听接口
    public interface OnItemLongClickListener {
        void onItemLongClick(View view, int position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mcontext = parent.getContext();
        if (viewType == TYPE_HEADER) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_psnheader, parent, false);
            RecyclerView.ViewHolder holder = new HeaderViewHolder(view);
            return holder;
        } else if (viewType == TYPE_CONTENT) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_personalinfo_item, parent, false);
            RecyclerView.ViewHolder holder = new ContentViewHolder(view);
            return holder;
        } else if (viewType == TYPE_SPLIT) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_personalinfo_item, parent, false);
            RecyclerView.ViewHolder holder = new ContentViewHolder(view);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(view.getLayoutParams());
            //列表项之间隔开
            lp.setMargins(0, 0, 0, 40);
            view.setLayoutParams(lp);
            view.setBackgroundResource(R.drawable.border_top_bottom);
            return holder;
        }
        return null;

    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return TYPE_HEADER;
        else if (position % 2 == 0)
            return TYPE_SPLIT;
        else
            return TYPE_CONTENT;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeaderViewHolder) {
            Object[] ojs = (Object[]) list.get(position);
            //加载圆形图像
            if (ojs[0].toString().indexOf("http") != -1) {
                Glide
                        .with(mcontext)
                        .load(ojs[0].toString())
                        .asBitmap().centerCrop()
                        .into(new GlideCircularLoadUtils(mcontext, ((HeaderViewHolder) holder).headImg));
            } else {
                Glide
                        .with(mcontext)
                        .load((int) ojs[0])
                        .asBitmap().centerCrop()
                        .into(new GlideCircularLoadUtils(mcontext, ((HeaderViewHolder) holder).headImg));
            }
            ((HeaderViewHolder) holder).headTitle.setText((String) ojs[1]);
            ((HeaderViewHolder) holder).headContent.setText((String) ojs[2]);
            ((HeaderViewHolder) holder).headCredit.setText(ojs[3].toString());
            ((HeaderViewHolder) holder).headFollow.setText(ojs[4].toString());
            ((HeaderViewHolder) holder).headFan.setText(ojs[5].toString());
        } else if (holder instanceof ContentViewHolder) {
            final PersonalInfoModel psnInfo = (PersonalInfoModel) list.get(position);
            ((ContentViewHolder) holder).psnIcon.setImageResource(psnInfo.getImage());
            ((ContentViewHolder) holder).psnTitle.setText(psnInfo.getName());
            ((ContentViewHolder) holder).psnArrow.setImageResource(psnInfo.getIcon());
        }

        //判断是否设置了监听器
        //单击判断
        if (mOnItemClickListener != null) {
            //为ItemView设置监听器
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.itemView, position);
                }
            });
        }
        //长按判断
        if (mOnItemLongClickListener != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemLongClickListener.onItemLongClick(holder.itemView, position);
                    //返回true 表示消耗了事件 事件不会继续传递
                    return true;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ContentViewHolder extends RecyclerView.ViewHolder {
        ImageView psnIcon;
        TextView psnTitle;
        ImageView psnArrow;

        public ContentViewHolder(View view) {
            super(view);
            psnIcon = (ImageView) view.findViewById(R.id.item_image);
            psnTitle = (TextView) view.findViewById(R.id.item_name);
            psnArrow = (ImageView) view.findViewById(R.id.item_arrow);
        }
    }

    public PersonalInfoAdapter(List<Object> psnlist) {
        list = psnlist;
    }

    //头部 ViewHolder
    public static class HeaderViewHolder extends RecyclerView.ViewHolder {
        ImageView headImg;
        TextView headTitle;
        TextView headContent;
        TextView headCredit;
        TextView headFan;
        TextView headFollow;
        LinearLayout headBack;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            headImg = (ImageView) itemView.findViewById(R.id.head_img);
            headTitle = (TextView) itemView.findViewById(R.id.head_nickname);
            headContent = (TextView) itemView.findViewById(R.id.head_account);
            headCredit=(TextView)itemView.findViewById(R.id.head_credit);
            headBack = (LinearLayout) itemView.findViewById(R.id.psn_data);
            headFan=(TextView)itemView.findViewById(R.id.head_fan);
            headFollow=(TextView)itemView.findViewById(R.id.head_follow);
//            headBack.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Intent intent=new Intent(mcontext,PsnDataActivity.class);
//                    mcontext.startActivity(intent);
//                }
//            });
        }
    }


}
